<template>
  <header class="header">
    <!-- 头部的第一行 -->
    <div class="top">
      <div class="container">
        <div class="loginList">
          <p>尚品汇欢迎您！</p>
          <p v-if="!userName">
            <span>请</span>
            <!-- 声明式导航 -->
            <router-link to="/Login">登录</router-link>
            <router-link class="register" to="/Register">免费注册</router-link>
          </p>
          <p v-else>
            <a>{{userName}}</a>
            <a class="register" @click="logout">退出登录</a>
          </p> 
        </div>
        <div class="typeList">
          <a @click="toCenter">我的订单</a>
          <a @click="toShopCart">我的购物车</a>
          <a href="###">我的尚品汇</a>
          <a href="###">尚品汇会员</a>
          <a href="###">企业采购</a>
          <a href="###">关注尚品汇</a>
          <a href="###">合作招商</a>
          <a href="###">商家后台</a>
        </div>
      </div>
    </div>
    <!--头部第二行 搜索区域-->
    <div class="bottom">
      <h1 class="logoArea">
        <router-link class="logo" to="/Home">
          <img src="./images/尚品汇gmall.png" alt="" />
        </router-link>
      </h1>
      <div class="searchArea">
        <form action="###" class="searchForm">
          <input
            type="text"
            id="autocomplete"
            class="input-error input-xxlarge"
            v-model="keyword"
            @keyup.enter="goSearch"
          />
          <button 
          class="sui-btn btn-xlarge btn-danger" 
          type="button" 
          @click="goSearch"
          >
            搜索
          </button>
        </form>
      </div>
    </div>
  </header>
</template>
<script>
export default {
  data() {
    return {
      keyword:''
    };
  },
  created() {},
  computed: {
    userName(){
      return this.$store.state.user.userInfo.name
    }
  },
  methods: {
    goSearch(){
      //搜索按钮的回调函数
      // this.$router.push('/Search')
      //路由传参
      //代表的是如果有query参数也带过去
      if (this.$route.query) {
        let loction = {
          name: "search",
          params: { keyword: this.keyword || undefined },
        };
        loction.query = this.$route.query;
        this.$router.push(loction);
      }
    },
    // 退出登录
    async logout(){
      // 通知服务器清除token 、用户信息
      try {
        await this.$store.dispatch('userLogout')
        this.$router.push('/Home')
      } catch (error) {
        alert('退出登录失败')
      }
    },
    // 去详情页
    toCenter(){
      this.$router.push('/center')
    },
    // 去购物车
    toShopCart(){
      this.$router.push('/ShopCart')
    }
  },
  mounted(){
    this.$bus.$on("clear",()=>{
      this.keyword = ''
    })
  }
};
</script>
<style lang="less" scoped>
    .header {
        &>.top {
            background-color: #f7b1b1;
            height: 30px;
            line-height: 30px;

            .container {
                width: 1200px;
                margin: 0 auto;
                overflow: hidden;

                .loginList {
                    float: left;

                    p {
                        float: left;
                        margin-right: 10px;
                        color: black;
                        .register {
                            border-left: 1px solid #b3aeae;
                            padding: 0 5px;
                            margin-left: 5px;
                        }
                    }
                }

                .typeList {
                    float: right;

                    a {
                        padding: 0 10px;
                        text-decoration-line: none;
                        color: black;
                        cursor: pointer;
                        &+a {
                            border-left: 1px solid #b3aeae;
                        }
                    }

                }

            }
        }

        &>.bottom {
            width: 1200px;
            margin: 0 auto;
            overflow: hidden;
            
            .logoArea {
                float: left;

                .logo {
                    img {
                        width: 135px;
                        height: 135px;
                        margin-left: 30px;
                    }
                }
            }

            .searchArea {
                float: right;
                margin-top: 50px;
                
                .searchForm {
                    overflow: hidden;

                    input {
                        box-sizing: border-box;
                        width: 490px;
                        height: 32px;
                        padding: 0px 4px;
                        border: 2px solid #2280dd;
                        float: left;

                        &:focus {
                            outline: none;
                        }
                    }

                    button {
                        height: 32px;
                        width: 68px;
                        font-size: 16px;
                        background-color: #2280dd;
                        border: none;
                        color: #fff;
                        float: left;
                        cursor: pointer;

                        &:focus {
                            outline: none;
                        }
                    }
                }
            }
        }
    }
</style>
